package com.hxexp.weixin.mvc.order.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.hpe.api.orderdetail.OrderDetailAPI;
import com.hxexp.weixin.hpe.api.orderdetail.model.OrderDetailParamBean;
import com.hxexp.weixin.hpe.api.orderdetail.model.OrderDetailResultBean;
import com.hxexp.weixin.hpe.api.ordersubmit.OrderSubmitAPI;
import com.hxexp.weixin.hpe.api.ordersubmit.model.OrderSubmitParamBean;
import com.hxexp.weixin.hpe.api.ordersubmit.model.OrderSubmitResultBean;
import com.hxexp.weixin.hpe.api.serviceprice.ServicePriceAPI;
import com.hxexp.weixin.hpe.api.serviceprice.model.ServicePriceParamBean;
import com.hxexp.weixin.hpe.api.serviceprice.model.ServicePriceResultBean;
import com.hxexp.weixin.hpe.api.userorderlist.UserOrderListAPI;
import com.hxexp.weixin.hpe.api.userorderlist.model.UserOrderListParamBean;
import com.hxexp.weixin.hpe.api.userorderlist.model.UserOrderListResultBean;
import com.hxexp.weixin.mvc.order.service.OrderService;

/**
 * 订单管理实现类
 *
 * @author wang.bin
 * @author li.jiang
 */
@Service
public final class OrderServiceImpl implements OrderService {
    @Autowired
    private UserOrderListAPI orderListAPI;
    @Autowired
    private ServicePriceAPI servicePriceAPI;
    @Autowired
    private OrderSubmitAPI orderSubmitAPI;
    @Autowired
    private OrderDetailAPI orderDetailAPI;

    @Override
    public UserOrderListResultBean getUserOrderList(String userId, int pageSize, int pageNow) throws ServiceException {
        UserOrderListParamBean userOrderListParamBean = new UserOrderListParamBean();
        UserOrderListResultBean userOrderListResultBean = new UserOrderListResultBean();
        userOrderListParamBean.setUserId(userId);
        userOrderListParamBean.setPageSize(pageSize);
        userOrderListParamBean.setPageNow(pageNow);

        try {
            userOrderListResultBean = orderListAPI.getOrderList(userOrderListParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return userOrderListResultBean;
    }

    @Override
    public String getServicePrice(String startStationId, String price, String priceType, String arriveStationId, String crossProvinceFlag)
            throws ServiceException {
        ServicePriceParamBean servicePriceParamBean = new ServicePriceParamBean();
        servicePriceParamBean.setStartStationId(startStationId);
        servicePriceParamBean.setPrice(price);
        servicePriceParamBean.setPriceType(priceType);
        servicePriceParamBean.setArriveStationId(arriveStationId);
        servicePriceParamBean.setCrossProvinceFlag(crossProvinceFlag);

        ServicePriceResultBean servicePriceResultBean = new ServicePriceResultBean();
        try {
            servicePriceResultBean = servicePriceAPI.getServiceCharge(servicePriceParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return servicePriceResultBean.getServicePrice();
    }

    @Override
    public OrderSubmitResultBean orderSubmit(OrderSubmitParamBean orderSubmitParamBean) throws ServiceException {
        OrderSubmitResultBean orderSubmitResultBean = new OrderSubmitResultBean();

        try {
            orderSubmitResultBean = orderSubmitAPI.orderSubmit(orderSubmitParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return orderSubmitResultBean;
    }

    @Override
    public OrderDetailResultBean getOrderDetail(String orderId) throws ServiceException {
        OrderDetailResultBean orderDetailResultBean = new OrderDetailResultBean();
        OrderDetailParamBean orderDetailParamBean = new OrderDetailParamBean();
        orderDetailParamBean.setOrderId(orderId);

        try {
            orderDetailResultBean = orderDetailAPI.getOrderDetail(orderDetailParamBean);
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return orderDetailResultBean;
    }

}
